package com.zhuss.core.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.core.permission.SearchPermissionBo;
import com.zhuss.bean.vo.core.permission.DetailPermissionVo;
import com.zhuss.bean.vo.core.permission.ListPermissionTreeBean;
import com.zhuss.bean.vo.core.permission.ListPermissionTreeVo;
import com.zhuss.bean.vo.core.permission.ListPermissionVo;
import com.zhuss.common.PageVo;
import com.zhuss.core.db.dao.ext.PermissionDao;
import com.zhuss.core.db.entity.Permission;
import com.zhuss.enums.EnumUtil;
import com.zhuss.enums.SystemCHEnum;
import com.zhuss.exception.AabException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.ListBean;


/**
 * 查询权限点 processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("permissionRead")
public class PermissionRead {
	
	@Autowired
	private PermissionDao permissionDao;
	
	/**
	 * 权限点树
	 * @param system
	 * @return
	 */
	public Object listPermissionTree(Integer system) {
		List<Permission> permissionList = permissionDao.listAll(system);
		List<ListPermissionTreeVo> result = new ArrayList<>();
		transferTree(permissionList, result);
		return result;
	}
	
	/**
	 * 转换权限点树
	 * @param permissionList
	 * @param result
	 */
	private void transferTree(List<Permission> permissionList, List<ListPermissionTreeVo> result) {
		for (Permission permission : permissionList) {
			if (permission.getParent() == null || permission.getParent().equals(0l)) {
				ListPermissionTreeVo vo = new ListPermissionTreeVo();
				vo.setLabel(permission.getName());
				vo.setValue(permission.getPermissionId());
				vo.setSystemName(EnumUtil.containsKey(SystemCHEnum.class, permission.getSystem()));
				result.add(vo);
			}
		}
		
		for (ListPermissionTreeVo vo : result) {
			vo.setChildren(init(permissionList, vo));
		}
	}
	
	/**
	 * 递归初始化权限点树
	 * @param permissionList
	 * @param vo
	 * @return
	 */
	private List<ListPermissionTreeVo> init(List<Permission> permissionList, ListPermissionTreeVo vo) {
		ListBean<ListPermissionTreeVo> listBean = new ListBean<>();
		for (Permission permission : permissionList) {
			if (vo.getValue().equals(permission.getParent())) {
				ListPermissionTreeVo item = new ListPermissionTreeVo();
				item.setValue(permission.getPermissionId());
				item.setLabel(permission.getName());
				item.setChildren(init(permissionList, item));
				listBean.build(item);
			}
		}
		return listBean.getList();
	}
	
	/**
	 * 查询权限点列表
	 * @param bo
	 * @return
	 * @throws AabException 
	 */
	public Object list(SearchPermissionBo bo) throws AabException {
		PageVo<ListPermissionVo> result = new PageVo<ListPermissionVo>();
        result.setPageNum(bo.getPageNum());
        result.setPageSize(bo.getPageSize());
        result.setItems(transferToList(permissionDao.list(bo)));
        result.setTotal(permissionDao.countList(bo));
		return result;
	}
	
	private List<ListPermissionVo> transferToList(List<ListPermissionVo> list) throws AabException {
		for (ListPermissionVo item : list) {
			Long parent = item.getParent();
			if (parent == null || parent.equals(0l)) {
				item.setParentName("");
				continue;
			}
			item.setParentName(((DetailPermissionVo) detail(parent)).getName());
		}
		return list;
	}
	
	/**
	 * 查询权限点详情
	 * @param permissionId
	 * @return
	 * @throws AabException
	 */
	public Object detail(Long permissionId) throws AabException {
		Object result = permissionDao.detail(permissionId);
		if (result == null) {
			throw new AabException(ExceptionCode.数据不存在);
		}
		List<Long> parentList = new ArrayList<Long>();
		listParent(permissionId, parentList);
		((DetailPermissionVo) result).setParentList(parentList);
		return result;
	}
	
	/**
	 * 查询权限点所有父级
	 * @param permissionId
	 * @param parentList
	 */
	public void listParent(Long permissionId, List<Long> parentList) {
		Long parent = permissionDao.listParent(permissionId);
		if (parent != null && !parent.equals(0l)) {
			parentList.add(parent);
			listParent(parent, parentList);
		}
		return;
		
	}
	
	/**
	 * 查询权限点子集
	 * @param permissionId
	 * @return
	 */
	public List<Permission> listSon(Long permissionId) {
		List<Permission> result = permissionDao.listSon(permissionId);
		return result;
	}
	
	
	/**
	 * 查询权限树 
	 * @param system
	 * @return
	 */
	public Object listPermissionTreeBean(Integer system) {
		List<Permission> permissionList = permissionDao.listAll(system);
		List<ListPermissionTreeBean> result = new ArrayList<>();
		transferPermissionTree(permissionList, result);
		return result;
	}
	
	
	/**
	 * 根据名字查询权限
	 * @param 
	 * @return
	 */
	public List<Permission> list(String name) {
		List<Permission> result = permissionDao.listByName(name);
		return result;
	}
	
	public List<Permission> listNotId(String name, Long permissionId) {
		List<Permission> result = new ArrayList<Permission>();
		List<Permission> list = list(name);
		for (Permission c : list) {
			if (!c.getPermissionId().equals(permissionId)) {
				result.add(c);
				break;
			}
		}
		return result;
	}
	
	/**
	 * 转换菜单资源混合树
	 * @param menuList
	 * @param result
	 */
	private void transferPermissionTree(List<Permission> permissionList,
			List<ListPermissionTreeBean> result) {
		List<ListPermissionTreeVo> tree = new ArrayList<>();
		transferTree(permissionList, tree);
		to(tree, result);
	}
	
	/**
	 * 递归转换
	 * @param tree
	 * @param result
	 * @return
	 */
	private List<ListPermissionTreeBean> to(List<ListPermissionTreeVo> tree, List<ListPermissionTreeBean> result) {
		if (result == null) {
			result = new ArrayList<>();
		}
		for (ListPermissionTreeVo p : tree) {
			ListPermissionTreeBean r = new ListPermissionTreeBean();
			r.setId(p.getValue());
			r.setLabel(p.getLabel());
			r.setSystemName(p.getSystemName());
			r.setChildren(to(p.getChildren(), r.getChildren()));
			result.add(r);
		}
		return result;
		
	}
	
	
	

}
